package com.zq.ht.web.controller.content;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zq.ht.common.util.DataGrid;
import com.zq.ht.common.util.PageUtil;
import com.zq.ht.common.util.Result;
import com.zq.ht.model.TNews;
import com.zq.ht.web.service.content.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by admin on 2016/6/21.
 */
@Controller
public class NewsController {
    @Autowired
    private NewsService newsService;

    /**
     * 新闻管理页
     * @return
     */
    @RequestMapping(value = "news_page")
    public String newsPage(){
        return "news/newsInfo";
    }

    /**
     * 新闻管理数据
     * @param request
     * @return
     */
    @RequestMapping(value = "news_page_info")
    @ResponseBody
    public String newsInfo(HttpServletRequest request){
        DataGrid grid = new DataGrid();
        try {
            PageUtil pageUtil = new PageUtil(request);
            pageUtil = newsService.getNewsList(pageUtil);
            grid.setRows(pageUtil.getResults());
            grid.setTotal(pageUtil.getTotalRows());
        }catch (Exception e){
            e.printStackTrace();
        }
        return JSONObject.toJSONString(grid);
    }

    /**
     * 编辑新闻页
     * @param id
     * @return
     */
    @RequestMapping(value = "news_edit")
    public String newsEdit(@RequestParam(value = "id",required = false) String id, ModelMap map){
        if(id!=null && !id.equals("")) {
            newsService.getNewsById(Long.parseLong(id), map);
        }
        return "news/newsEdit";
    }

    /**
     * 保存新闻
     * @param data
     * @param content
     * @return
     */
    @RequestMapping(value = "news_save")
    public @ResponseBody String newsSave(String data,String content){
        Result result = new Result();
        try{
            TNews news = JSON.parseObject(data,TNews.class);
            if(news.getId()==null || news.getId().equals("")){
                newsService.saveNews(news,content);
            }else{
                newsService.updateNews(news,content);
            }

            result.setSuccess(true);
            result.setMsg("新闻保存成功。");
        }catch (Exception e){
            e.printStackTrace();
        }
        return JSON.toJSONString(result);
    }

    /**
     * 删除新闻
     * @param id
     * @return
     */
    @RequestMapping(value = "news_del")
    public @ResponseBody String newsDel(int id){
        Result result = new Result();
        try{
            newsService.deleteNews(id);
            result.setSuccess(true);
            result.setMsg("删除成功。");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMsg("删除失败。");
            e.printStackTrace();
        }
        return JSON.toJSONString(result);
    }
}
